package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jyd
 * @since 2022-03-22
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;
    @GetMapping("getChapterAndVideo/{courseId}")
    public R getChapterAndVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterAndVideo(courseId);
        if(list != null){
            return R.ok().data("allChapterVideo",list);
        }else {
            return R.error().message("查询失败");
        }
        //return R.ok().data("list",list);
    }
    //根据id查询章节
    @GetMapping("getChapterById/{chapterId}")
    public R getChapterById(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }
    //添加章节
    @PostMapping("addChapter")
    public R saveChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }
    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }
    //删除章节
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean result = chapterService.deleteChapter(chapterId);
        if(result){
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }
}

